﻿using System;
using System.Collections.Generic;
using System.Linq;
using Fuse8.ContentManagementFramework.Domain.Commands.CmsUser;
using Fuse8.ContentManagementFramework.Domain.Commands.CommandParameters.CmsUser;
using Fuse8.DomainFramework.Common;
using Fuse8.DomainFramework.Common.Locator;
using Fuse8.DomainFramework.BusinessService;

using Fuse8.ContentManagementFramework.Domain;
using Fuse8.ContentManagementFramework.Domain.Special;
using Fuse8.ContentManagementFramework.Domain.Commands;
using Fuse8.ContentManagementFramework.Domain.Commands.CommandParameters;

namespace Fuse8.ContentManagementFramework.BizFacade.Services
{
    public class CmsUserService : CmsBusinessService<CmsUser>
    {
        public virtual IEnumerable<CmsUser> GetForUserGroup(CmsUserGroup userGroup)
        {
            Guard.ArgumentNotNull(userGroup, "userGroup");

            return GetForUserGroup(userGroup.Id);
        }

        public virtual IEnumerable<CmsUser> GetForUserGroup(int? userGroupId)
        {
            var command = DependencyServiceLocator.Current.GetInstance<CmsUserGetByUserGroupCommand>();

            var commandParameter = new CmsUserGetByUserGroupCommandParameter
            {
                UserGroupId = userGroupId
            };

            SiteAwareEntityObjectMediator.SetSiteEntity(commandParameter);

            command.ConfigureCommand(commandParameter);

            return Find(command);
        }

        public virtual CmsUser GetByEmail(string email)
        {
            var command = DependencyServiceLocator.Current.GetInstance<CmsUserGetByEmailCommand>();

            var commandParameter = new CmsUserGetByEmailCommandParameter
            {
                Email = email
            };

            SiteAwareEntityObjectMediator.SetSiteEntity(commandParameter);

            command.ConfigureCommand(commandParameter);

            return Find(command).FirstOrDefault();
        }
    }
}
